﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace GoogleVoice
{
    public static class Constants
    {
        public static readonly String REG_PATH = @"HKEY_CURRENT_USER\Software\GoogVoice-WinMo";
        public static readonly String gvPHP = "http://server/index.php/gv/call"; // Set this the the GoogVoice-PHP server URL
    }
    /// <summary>
    /// Provides some simple utilities for phone stuff.
    /// </summary>
    public static class PhoneUtils
    {
        /// <summary>
        /// Strips out the extra crap from a string containing a phone number. Leaves only the numbers.
        /// </summary>
        /// <param name="numberText">A phone number string such as +1(865) 555-1212</param>
        /// <returns>Returns the phone string stripped like 18655551212</returns>
        public static String CleanPhoneText(String numberText)
        {
            return Regex.Replace(numberText, @"\D*", "");
        }
        /// <summary>
        /// Formats the cleaned phone text string into a US number format. +#...# (###) ###-####
        /// </summary>
        /// <param name="phoneText">A cleaned phone number string like 18655551212</param>
        /// <returns>A US formatted phone string like +1 (865) 555-1212</returns>
        public static String FormatPhoneText(String phoneText)
        {
            int len = phoneText.Length;
            if (len > 10)
            {
                int area = len - 10;
                int pre = area + 3;
                int post = pre + 3;
                String cc = phoneText.Substring(0, area);
                String ac = phoneText.Substring(area, 3);
                String p1c = phoneText.Substring(pre, 3);
                String p2c = phoneText.Substring(post, 4);
                return String.Format("+{0} ({1}) {2}-{3}", cc, ac, p1c, p2c);
            }
            else if (len > 7)
            {
                int pre = len - 7;
                int post = pre + 3;
                String ac = phoneText.Substring(0, pre);
                String p1c = phoneText.Substring(pre, 3);
                String p2c = phoneText.Substring(post);
                return String.Format("({0}) {1}-{2}", ac, p1c, p2c);
            }
            else if (len > 4)
            {
                int post = len - 4;
                String p1c = phoneText.Substring(0, post);
                String p2c = phoneText.Substring(post);
                return String.Format("{0}-{1}", p1c, p2c);
            }
            else
            {
                return phoneText;
            }
        }
    }
}
